package com.algorithm.queue;
/*
 *@Author:DOWN
 *@Date:2021/12/27 11:45
 *@Comment:
 */

import java.util.Stack;

public class StackRealizeQueue {
    int front=0;
    Stack<Integer> stack1,stack2;
    public StackRealizeQueue() {
        stack1=new Stack<>();
        stack2=new Stack<>();
    }

    /**
     * 将元素 x 推到队列的末尾
     * @param x int
     */
    public void push(int x) {
        if(stack1.isEmpty()){
            front=x;
        }
        stack1.push(x);
    }

    /**
     * 从队列的开头移除并返回元素
     * @return int
     */
    public int pop() {
        if(!stack2.isEmpty()){
            return stack2.pop();
        }
        while(stack1.size()>1){
            stack2.push(stack1.pop());
        }
        return stack1.pop();
    }

    /**
     * 返回队列开头的元素
     * @return int
     */
    public int peek() {
        if(!stack2.isEmpty()){
            return stack2.peek();
        }
        return front;
    }

    /**
     * 如果队列为空，返回 true ；否则，返回 false
     * @return boolean
     */
    public boolean empty() {
        return this.stack1.isEmpty()&&this.stack2.isEmpty();
    }

}
